
class Task {
  constructor ({ gantt, vue, VueGantt }) {
    this.gantt = gantt
    this.vue = vue
    this.VueGantt = VueGantt
  }

  registerEvent () {
    this.gantt.attachEvent('onAfterTaskDelete', (id, item) => {
      this.setLineData('onAfterTaskDelete')
    })
    this.gantt.attachEvent('onAfterTaskAdd', (id, item) => {
      this.setLineData('onAfterTaskAdd')
    })
    this.gantt.attachEvent('onAfterTaskUpdate', (id, item) => {
      this.setUpdate(item)
      console.log(item, 'azzzzz')
    })
  }

  setUpdate (target) {
    const { task } = this.VueGantt
    const source = task.data.find(v => v.id === target.id)
    if (source) {
      for (const key in target) {
        source[key] = target[key]
      }
    }
    this.vue.$emit('input', task)
    this.vue.$emit('onAfterTaskUpdate', task.data)
  }

  setLineData (ev) {
    const { task } = this.VueGantt
    task.data = this.VueGantt.getDataTaskStore()
    this.vue.$emit('input', task)
    this.vue.$emit(ev, task.data)
  }

  init () {
    this.registerEvent()
  }
}

export default Task
